﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace webapp4U.UI
{
    public partial class WebMaster_Default : System.Web.UI.Page
    {
        int AdvID = -1;
        string FileName = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            AdvID = Convert.ToInt32(Request.QueryString["AdvID"]);
            if (AdvID>0)
            {
                btSave.Text = " Update ";
                if (!IsPostBack)
                {
                    Load_GetAdvertiseByAdvID(AdvID);
                    Rqfvfileupload.Enabled = false;
                }
            }
        }

        protected string FilePhoto_Upload()
        {
            string strFileName = "";
            // kiem tra xem user co chon hinh dai dien ko
            if (PhotoFile.HasFile)
            {
                // kiem tra phan mo rong cua file co hop le hay khong
                string strExtension = System.IO.Path.GetExtension(PhotoFile.FileName).ToUpper();
                if (strExtension == ".JPG" || strExtension == ".GIF" || strExtension == ".JPEG" || strExtension == ".PNG" || strExtension == ".SWF")
                {
                    // lay ten file de khong bi trung file + phan mo rong cua file can upload
                    strFileName = Common.GetFileName() + System.IO.Path.GetExtension(PhotoFile.FileName);

                    //duong dan tao save file Large
                    string strFilePathLarge = Request.PhysicalApplicationPath + "UserUpload/Logo/" + strFileName;

                    //thuc hien save file Large len server
                    EnumUpload uploadLarge = FileProcess.UpLoadImage(PhotoFile, strFilePathLarge, -1);
                }
                else
                {
                    PanelMsg.Visible = true;
                    lblMsg.Text = "File bạn chọn upload không hợp lệ.";
                    strFileName = "";
                }
            }
            else
                strFileName = hImage.Value;

            return strFileName;
        }


        protected void Load_GetAdvertiseByAdvID(int AdvID)
        {
            webapp4U.BOL.Advertise obj = webapp4U.BOL.Advertise.GetAdvertiseByID(AdvID);
            txtUrl.Text = obj.AdvURL;
            txtOrder.Text = obj.DisplayOrder.ToString();
            dlPosition.SelectedValue = obj.AdvPosition.ToString();
            hImage.Value = obj.FileName;
            txtWidth.Text = obj.AdvWidth.ToString();
            txtHeight.Text = obj.AdvHeight.ToString();
            FileName = obj.FileName;
            dlCategory.SelectedValue = obj.CateID.ToString();
        }
        
        protected string GetAdvertise()
        {
            int Width = 130;
            int Height = 130;

            if (System.IO.Path.GetExtension(FileName).ToLower() == ".swf")
                return "<script>WriteObject('" + Globals.ApplicationPath + "UserUpload/Logo/" + FileName + "' ," + Width + "," + Height + ");</script>";
            else
            {
                Height = -1;

                if (Width > 0 && Height > 0)
                    return "<img src='" + Globals.ApplicationPath + "UserUpload/Logo/" + FileName + "' border=0 width=" + Width + " Height=" + Height + "   />";
                else if (Width > 0)
                    return "<img src='" + Globals.ApplicationPath + "UserUpload/Logo/" + FileName + "' border=0 width=" + Width + " />";
                else if (Height > 0)
                    return "<img src='" + Globals.ApplicationPath + "UserUpload/Logo/" + FileName + "' border=0 Height=" + Height + " />";
                else
                    return "<img src='" + Globals.ApplicationPath + "UserUpload/Logo/" + FileName + "' border=0 />";
            }
        }


        protected void btSave_Click(object sender, EventArgs e)
        {
            // cap nhat du lieu vao database
            webapp4U.BOL.Advertise obj = new webapp4U.BOL.Advertise();
            obj.AdvID = AdvID;
            obj.AdvURL = txtUrl.Text;
            obj.RegionDate = DateTime.Now;
            obj.ExpireDate = DateTime.MaxValue;

            if (txtRegionDate.Text != "")
                obj.RegionDate = Convert.ToDateTime(txtRegionDate.Text);
            if (txtExpireDate.Text != "")
                obj.ExpireDate = Convert.ToDateTime(txtExpireDate.Text);

            obj.AdvPosition = Convert.ToInt32(dlPosition.SelectedItem.Value);
            obj.DisplayOrder = Globals.ConvertToInt32(txtOrder.Text);
            obj.AddedDate = DateTime.Now;
            obj.IsActive = true;
            obj.AdvWidth = Globals.ConvertToInt32(txtWidth.Text);
            obj.AdvHeight = Globals.ConvertToInt32(txtHeight.Text);
            obj.FileName = FilePhoto_Upload();
            obj.CateID = Convert.ToInt32(dlCategory.SelectedItem.Value);

            if (Globals.GetIntFromQueryString("AdvID")>0)
            {
                webapp4U.BOL.Advertise.Update(obj);
            }
            else
            {
                webapp4U.BOL.Advertise.Insert(obj);
            }
            Response.Redirect(Globals.ApplicationPath + "WebMaster/Advertise");
        }
    }
}